*********************************************************************************************
*****	Table 4 Individual-Level Hedonic Price Function Estimates: Weekend Control Group ****
*********************************************************************************************

capture log close
capture file close myfile
local cutoff=0.01									// where to truncate reliability (0 or 0.01)
set seed 1234
log using ".\results\maintablesfigs\tabs\Table3.txt", text replace
putexcel set ".\results\maintablesfigs\maintextFigsTabs.xlsx", sheet(Table3) modify
putexcel B2="I"
putexcel D2="II"
putexcel F2="III"
putexcel B3 ="Full Sample"
putexcel D3 ="Full Sample"
putexcel F3 ="After Oct. 20th, 2013"
putexcel A4 = "Constant"
putexcel A8 = "1(Morning Peak)"
putexcel A12 = "Travel Time"
putexcel A16 = "Travel Time x Morning Peak"
putexcel A20 = "Reliability"
putexcel A24 = "Reliability x Morning Peak"
putexcel A28 = "Number of Accounts"
***************************
***************************
** Weekend Control Group **
***************************
***************************

clear
save .\data\temp\temp1, replace emptyok

* Get reliability
*I 10 W

use .\data\clean\HV_ML_reliab, clear
keep if fwy==10&dir=="W"
format date %d
sort date hour
save .\data\temp\temp, replace

use .\data\clean\I10W_laneuse_dataset_15nov14_wcensus, clear
sort date hour
merge m:1 date hour using .\data\temp\temp, keep(1 3) nogen 
*drop if weekend==1
keep if inlist(hour,5,6,7,8)|weekend==1
drop if holiday==1
drop if transponder==""
drop if transponder=="."
*WTP calculation
gen EL_TT=dist/ELspeed
gen ML_TT=dist/MLspeed
gen TT_dif_hr=ML_TT-EL_TT
gen WTP2=charged_toll/TT_dif_hr

gen reliabilityML=dist/p20_speedML-dist/p50_speedML
gen reliabilityHV=dist/p20_speedHV-dist/p50_speedHV
gen reliability_diff=reliabilityML-reliabilityHV
replace reliability_diff=0 if reliability_diff<`cutoff'
keep if WTP2>0&WTP2~=.
keep if reliability_diff~=.
drop if ELspeed==.
drop if TT_dif_hr==.
keep if acct_type=="PRIVATE"&occupancy~="HOV-3"

gen MP=inlist(hour,5,6,7,8)&weekend==0
gen TT_dif_hrMP=MP*TT_dif_hr
gen ReliabilityMP=MP*reliability_diff

sort transponder
preserve
keep acct_no
duplicates drop
sort acct_no
outsheet acct_no using .\data\temp\temp.txt, replace comma non noq
restore

* II.A.b.1. Read Station List 
file open myfile using .\data\temp\temp.txt, read
file read myfile line

while r(eof)==0 { /* while you're not at the end of the file */
	*di "`line'"
	qui {
		preserve
			keep if acct_no=="`line'"
			total weekend
			mat b=e(b)
			local A=b[1,1] 
			total MP
			mat b=e(b)
			local B=b[1,1]
			if `A'>=5&`B'>=5 {
			reg charged_toll MP TT_dif_hr TT_dif_hrMP , cluster(rt_id)
			matrix   b1=get(_b)
			drop _all
			set obs 1
			gen acct_no="`line'"
			gen urgency=_b[_cons]
			gen urgencyse=_se[_cons]
			gen MP_const=_b[MP]
			gen MP_constse=_se[MP]
			gen VOTMP=_b[TT_dif_hrMP]
			gen VOTMPse=_se[TT_dif_hrMP]
			gen VOTW=_b[TT_dif_hr]
			gen VOTWse=_se[TT_dif_hr]

			append using .\data\temp\temp1
			save .\data\temp\temp1, replace
			}
		restore
		}
	display "." _cont
	file read myfile line
	}

file close myfile
use .\data\temp\temp1, clear
save .\data\temp\acct_by_acct_weekend_vot, replace


drop if urgency==0
drop if MP_const==0
drop if VOTMP==0
drop if VOTW==0


drop if urgencyse==0
drop if MP_constse==0
drop if VOTMPse==0
drop if VOTWse==0


drop if urgencyse==.
drop if MP_constse==.
drop if VOTMPse==.
drop if VOTWse==.




sum urgency, detail
local VOU=round(`r(mean)',0.01)
local VOU25=round(`r(p25)',0.01)
local VOU75=round(`r(p75)',0.01)
local N=`r(N)'
sum MP_const, detail
local MP_const=round(`r(mean)',0.01)
local MP_const25=round(`r(p25)',0.01)
local MP_const75=round(`r(p75)',0.01)
sum VOTMP, detail
local VOTMP=round(`r(mean)',0.01)
local VOTMP25=round(`r(p25)',0.01)
local VOTMP75=round(`r(p75)',0.01)
sum VOTW, detail
local VOTW=round(`r(mean)',0.01)
local VOTW25=round(`r(p25)',0.01)
local VOTW75=round(`r(p75)',0.01)



* 95 CI
save ".\data\temp\temp3", replace
clear
set obs 500
gen urgency=0
gen MP_const=0
gen VOTMP=0
gen VOTW=0

save ".\data\temp\CI95_MP10_vot", replace emptyok
*relMP rel
foreach j of varlist urgency MP_const VOTMP VOTW {
	forvalues i=1(1)500 {
		qui use ".\data\temp\temp3", clear
		qui gen temp1=rnormal(`j',`j'se)
		qui sum temp1
		local h=r(mean)
		qui use ".\data\temp\CI95_MP10_vot", clear
		qui replace `j'=`h' if _n==`i'
		qui save ".\data\temp\CI95_MP10_vot", replace
		}
	}
sum urgency
local VOUse=round(`r(sd)',0.01)
sum MP_const
local MP_constse=round(`r(sd)',0.01)

sum VOTW
local VOTWse=round(`r(sd)',0.01)
sum VOTMP
local VOTMPse=round(`r(sd)',0.01)

di "Full Sample"
di "`VOU'"
di "(`VOUse')"
di "[`VOU25',`VOU75']"
di ""
di "`MP_const'"
di "(`MP_constse')"
di "[`MP_const25',`MP_const75']"
di ""
di "`VOTW'"
di "(`VOTWse')"
di "[`VOTW25',`VOTW75']"
di ""
di "`VOTMP'"
di "(`VOTMPse')"
di "[`VOTMP25',`VOTMP75']"
di ""
di "Obs. `N'"



quietly{
	local i=4
	local j=`i'+1
	local k=`j'+1
	foreach v in  VOU MP_const VOTW VOTMP{
		*local `v'=
		putexcel B`i' = ``v''
		putexcel B`j' = "(``v'se')"
		putexcel B`k' = "[``v'25', ``v'75']"
		local i=`i'+4
		local j=`i'+1
		local k=`j'+1
		}
	putexcel B28=`N'
	}


***********************************************************
// Column II FULL SAMPLE WITH RELIABILITY 
***********************************************************

clear
save ".\data\temp\temp1", replace emptyok

* Get reliability
*I 10 W
use ".\data\clean\HV_ML_reliab", clear
keep if fwy==10&dir=="W"
format date %d
sort date hour
save ".\data\temp\temp2", replace


use ".\data\clean\I10W_laneuse_dataset_15nov14_wcensus", clear

sort date hour
merge m:1 date hour using ".\data\temp\temp2", keep(1 3) nogen 
keep if inlist(hour,5,6,7,8) |weekend==1
*keep if date>td(20oct2013)
drop if holiday==1
drop if transponder==""
drop if transponder=="."
*WTP calculation
gen EL_TT=dist/ELspeed
gen ML_TT=dist/MLspeed
gen MP=inlist(hour,5,6,7,8)&weekend==0
gen TT_dif_hr=ML_TT-EL_TT
gen WTP2=charged_toll/TT_dif_hr



gen reliabilityML=dist/p20_speedML-dist/p50_speedML
gen reliabilityHV=dist/p20_speedHV-dist/p50_speedHV
gen reliability_diff=reliabilityML-reliabilityHV
replace reliability_diff=0 if reliability_diff<`cutoff'

keep if WTP2>0&WTP2~=.
keep if reliability_diff~=.
drop if ELspeed==.
drop if TT_dif_hr==.
keep if acct_type=="PRIVATE"&occupancy~="HOV-3"

gen TT_dif_hrMP=MP*TT_dif_hr
gen ReliabilityMP=MP*reliability_diff

sort transponder
preserve
keep acct_no
duplicates drop
sort acct_no
outsheet acct_no using ".\data\temp\temp3.txt", replace comma non noq
restore

* II.A.b.1. Read Station List 
file open myfile using ".\data\temp\temp3.txt", read
file read myfile line

while r(eof)==0 { /* while you're not at the end of the file */
	*di "`line'"
	qui {
		preserve
			keep if acct_no=="`line'"
			total weekend
			mat b=e(b)
			local A=b[1,1] 
			total MP
			mat b=e(b)
			local B=b[1,1]
			if `A'>=5&`B'>=5 {
				reg charged_toll MP TT_dif_hr TT_dif_hrMP reliability_diff ReliabilityMP, cluster(rt_id)
				matrix   b1=get(_b)
				drop _all
				set obs 1
				gen acct_no="`line'"
				gen urgency=_b[_cons]
				gen urgencyse=_se[_cons]
				gen MP_const=_b[MP]
				gen MP_constse=_se[MP]
				gen VOTMP=_b[TT_dif_hrMP]
				gen VOTMPse=_se[TT_dif_hrMP]
				gen VOTW=_b[TT_dif_hr]
				gen VOTWse=_se[TT_dif_hr]	
				gen rel=_b[reliability_diff]
				gen relse=_se[reliability_diff]
				gen relMP=_b[ReliabilityMP]
				gen relMPse=_se[ReliabilityMP]
				append using ".\data\temp\temp1"
				save ".\data\temp\temp1", replace
				}
		restore
		}
	display "." _cont
	file read myfile line
	}

file close myfile
use ".\data\temp\temp1", clear
save ".\data\temp\acct_by_acct_MP10_wvor", replace

drop if urgency==0
drop if MP_const==0
drop if VOTMP==0
drop if VOTW==0
drop if relMP==0
drop if rel==0

drop if urgencyse==0
drop if MP_constse==0
drop if VOTMPse==0
drop if VOTWse==0
drop if relMPse==0
drop if relse==0

drop if urgencyse==.
drop if MP_constse==.
drop if VOTMPse==.
drop if VOTWse==.
drop if relMPse==.
drop if relse==.

sum urgency, detail
local VOU=round(`r(mean)',0.01)
local VOU25=round(`r(p25)',0.01)
local VOU75=round(`r(p75)',0.01)
local N=`r(N)'
sum MP_const, detail
local MP_const=round(`r(mean)',0.01)
local MP_const25=round(`r(p25)',0.01)
local MP_const75=round(`r(p75)',0.01)
sum VOTMP, detail
local VOTMP=round(`r(mean)',0.01)
local VOTMP25=round(`r(p25)',0.01)
local VOTMP75=round(`r(p75)',0.01)
sum VOTW, detail
local VOTW=round(`r(mean)',0.01)
local VOTW25=round(`r(p25)',0.01)
local VOTW75=round(`r(p75)',0.01)
sum rel, detail
local VORW=round(`r(mean)',0.01)
local VORW25=round(`r(p25)',0.01)
local VORW75=round(`r(p75)',0.01)
sum relMP, detail
local VORMP=round(`r(mean)',0.01)
local VORMP25=round(`r(p25)',0.01)
local VORMP75=round(`r(p75)',0.01)

* 95 CI
save ".\data\temp\temp3", replace
clear
set obs 500
gen urgency=0
gen MP_const=0
gen VOTMP=0
gen VOTW=0
gen relMP=0
gen rel=0
save ".\data\temp\CI95_MP10_wvor", replace emptyok
*relMP rel
foreach j of varlist urgency MP_const VOTMP VOTW rel relMP{
	forvalues i=1(1)500 {
		qui use ".\data\temp\temp3", clear
		qui gen temp1=rnormal(`j',`j'se)
		qui sum temp1
		local h=r(mean)
		qui use ".\data\temp\CI95_MP10_wvor", clear
		qui replace `j'=`h' if _n==`i'
		qui save ".\data\temp\CI95_MP10_wvor", replace
		}
	}
sum urgency
local VOUse=round(`r(sd)',0.01)
sum MP_const
local MP_constse=round(`r(sd)',0.01)
sum VOTW
local VOTWse=round(`r(sd)',0.01)
sum VOTMP
local VOTMPse=round(`r(sd)',0.01)
sum rel
local VORWse=round(`r(sd)',0.01)
sum relMP
local VORMPse=round(`r(sd)',0.01)

di "Full Sample"
di "`VOU'"
di "(`VOUse')"
di "[`VOU25',`VOU75']"
di ""
di "`MP_const'"
di "(`MP_constse')"
di "[`MP_const25',`MP_const75']"
di ""
di "`VOTW'"
di "(`VOTWse')"
di "[`VOTW25',`VOTW75']"
di ""
di "`VOTMP'"
di "(`VOTMPse')"
di "[`VOTMP25',`VOTMP75']"
di ""
di "`VORW'"
di "(`VORWse')"
di "[`VORW25',`VORW75']"
di ""
di "`VORMP'"
di "(`VORMPse')"
di "[`VORMP25',`VORMP75']"
di ""
di "Obs. `N'"


quietly{
	local i=4
	local j=`i'+1
	local k=`j'+1
	foreach v in  VOU MP_const VOTW VOTMP VORW VORMP{
		*local `v'=
		putexcel D`i' = ``v''
		putexcel D`j' = "(``v'se')"
		putexcel D`k' = "[``v'25', ``v'75']"
		local i=`i'+4
		local j=`i'+1
		local k=`j'+1
		}
	putexcel D28=`N'
	}


**********************************************************************
// Column III Post Oct 20 with Reliability
**********************************************************************




clear
save ".\data\temp\temp1", replace emptyok

* Get reliability
*I 10 W
use ".\data\clean\HV_ML_reliab", clear
keep if fwy==10&dir=="W"
format date %d
sort date hour
save ".\data\temp\temp2", replace


use ".\data\clean\I10W_laneuse_dataset_15nov14_wcensus", clear

sort date hour
merge m:1 date hour using ".\data\temp\temp2", keep(1 3) nogen 
keep if inlist(hour,5,6,7,8) |weekend==1
keep if date>td(20oct2013)
drop if holiday==1
drop if transponder==""
drop if transponder=="."
*WTP calculation
gen EL_TT=dist/ELspeed
gen ML_TT=dist/MLspeed
gen TT_dif_hr=ML_TT-EL_TT
gen WTP2=charged_toll/TT_dif_hr



gen reliabilityML=dist/p20_speedML-dist/p50_speedML
gen reliabilityHV=dist/p20_speedHV-dist/p50_speedHV
gen reliability_diff=reliabilityML-reliabilityHV
replace reliability_diff=0 if reliability_diff<`cutoff'
keep if WTP2>0&WTP2~=.
keep if reliability_diff~=.
drop if ELspeed==.
drop if TT_dif_hr==.
keep if acct_type=="PRIVATE"&occupancy~="HOV-3"

gen MP=inlist(hour,5,6,7,8)&weekend==0
gen TT_dif_hrMP=MP*TT_dif_hr
gen ReliabilityMP=MP*reliability_diff

sort transponder
preserve
keep acct_no
duplicates drop
sort acct_no
outsheet acct_no using ".\data\temp\temp3.txt", replace comma non noq
restore

* II.A.b.1. Read Station List 
file open myfile using ".\data\temp\temp3.txt", read
file read myfile line

while r(eof)==0 { /* while you're not at the end of the file */
	*di "`line'"
	qui {
		preserve
			keep if acct_no=="`line'"
			total weekend
			mat b=e(b)
			local A=b[1,1] 
			total MP
			mat b=e(b)
			local B=b[1,1]
			if `A'>=5&`B'>=5 {
				reg charged_toll MP TT_dif_hr TT_dif_hrMP reliability_diff ReliabilityMP, cluster(rt_id)
				matrix   b1=get(_b)
				drop _all
				set obs 1
				gen acct_no="`line'"
				gen urgency=_b[_cons]
				gen urgencyse=_se[_cons]
				gen MP_const=_b[MP]
				gen MP_constse=_se[MP]
				gen VOTMP=_b[TT_dif_hrMP]
				gen VOTMPse=_se[TT_dif_hrMP]
				gen VOTW=_b[TT_dif_hr]
				gen VOTWse=_se[TT_dif_hr]	
				gen rel=_b[reliability_diff]
				gen relse=_se[reliability_diff]
				gen relMP=_b[ReliabilityMP]
				gen relMPse=_se[ReliabilityMP]
				append using ".\data\temp\temp1"
				save ".\data\temp\temp1", replace
				}
		restore
		}
	display "." _cont
	file read myfile line
	}

file close myfile
use ".\data\temp\temp1", clear
save ".\data\temp\acct_by_acct_MP10_wvor_postoct", replace

drop if urgency==0
drop if MP_const==0
drop if VOTMP==0
drop if VOTW==0
drop if relMP==0
drop if rel==0

drop if urgencyse==0
drop if MP_constse==0
drop if VOTMPse==0
drop if VOTWse==0
drop if relMPse==0
drop if relse==0

drop if urgencyse==.
drop if MP_constse==.
drop if VOTMPse==.
drop if VOTWse==.
drop if relMPse==.
drop if relse==.


sum urgency, detail
local VOU=round(`r(mean)',0.01)
local VOU25=round(`r(p25)',0.01)
local VOU75=round(`r(p75)',0.01)
local N=`r(N)'
sum MP_const, detail
local MP_const=round(`r(mean)',0.01)
local MP_const25=round(`r(p25)',0.01)
local MP_const75=round(`r(p75)',0.01)
sum VOTMP, detail
local VOTMP=round(`r(mean)',0.01)
local VOTMP25=round(`r(p25)',0.01)
local VOTMP75=round(`r(p75)',0.01)
sum VOTW, detail
local VOTW=round(`r(mean)',0.01)
local VOTW25=round(`r(p25)',0.01)
local VOTW75=round(`r(p75)',0.01)
sum rel, detail
local VORW=round(`r(mean)',0.01)
local VORW25=round(`r(p25)',0.01)
local VORW75=round(`r(p75)',0.01)
sum relMP, detail
local VORMP=round(`r(mean)',0.01)
local VORMP25=round(`r(p25)',0.01)
local VORMP75=round(`r(p75)',0.01)

* 95 CI
save ".\data\temp\temp3", replace
clear
set obs 500
gen urgency=0
gen MP_const=0
gen VOTMP=0
gen VOTW=0
gen relMP=0
gen rel=0
save ".\data\temp\CI95_MP10_wvor_postoct", replace emptyok
*relMP rel
foreach j of varlist urgency MP_const VOTMP VOTW rel relMP{
	forvalues i=1(1)500 {
		qui use ".\data\temp\temp3", clear
		qui gen temp1=rnormal(`j',`j'se)
		qui sum temp1
		local h=r(mean)
		qui use ".\data\temp\CI95_MP10_wvor_postoct", clear
		qui replace `j'=`h' if _n==`i'
		qui save ".\data\temp\CI95_MP10_wvor_postoct", replace
		}
	}
sum urgency
local VOUse=round(`r(sd)',0.01)
sum MP_const
local MP_constse=round(`r(sd)',0.01)
sum VOTW
local VOTWse=round(`r(sd)',0.01)
sum VOTMP
local VOTMPse=round(`r(sd)',0.01)
sum rel
local VORWse=round(`r(sd)',0.01)
sum relMP
local VORMPse=round(`r(sd)',0.01)

di "Full Sample"
di "`VOU'"
di "(`VOUse')"
di "[`VOU25',`VOU75']"
di ""
di "`MP_const'"
di "(`MP_constse')"
di "[`MP_const25',`MP_const75']"
di ""
di "`VOTW'"
di "(`VOTWse')"
di "[`VOTW25',`VOTW75']"
di ""
di "`VOTMP'"
di "(`VOTMPse')"
di "[`VOTMP25',`VOTMP75']"
di ""
di "`VORW'"
di "(`VORWse')"
di "[`VORW25',`VORW75']"
di ""
di "`VORMP'"
di "(`VORMPse')"
di "[`VORMP25',`VORMP75']"
di ""
di "Obs. `N'"

quietly{
	local i=4
	local j=`i'+1
	local k=`j'+1
	foreach v in  VOU MP_const VOTW VOTMP VORW VORMP{
		*local `v'=
		putexcel F`i' = ``v''
		putexcel F`j' = "(``v'se')"
		putexcel F`k' = "[``v'25', ``v'75']"
		local i=`i'+4
		local j=`i'+1
		local k=`j'+1
		}
	putexcel F28=`N'
	}


log close
